<%@page import="com.sbdcpn.utils.db.BaseDAO"%>
<%
	String strSQL = "select sum(cntnum) as pagenum ,sum(goods_sum) as goods_sum,sum(goods_sum_tax) as goods_sum_tax, sum(goods_tax) as goods_tax,sum(in_sum) as in_sum from (select v_finance_carr_purc_deta.provider_code ,v_finance_carr_purc_deta.provider_id ,v_finance_carr_purc_deta.cost_unit_id ,v_finance_carr_purc_deta.material_diff,count(provider_id) as cntnum ,v_finance_carr_purc_deta.provider_id ,trim(to_char(sum(v_finance_carr_purc_deta.totle_cost_tax),'999999999.99')) as goods_sum_tax,trim(to_char(sum(totle_cost_tax-totle_cost),'999999999.99')) as goods_tax,trim(to_char(sum(v_finance_carr_purc_deta.totle_cost),'999999999.99')) as goods_sum,trim(to_char(sum(in_sum),'999999999.99')) as in_sum  from v_finance_carr_purc_deta ";
    //String strSQL = "select sum(cnt) as pagenum, sum(totle_cost) as goods_sum, sum(totle_cost_tax) as goods_sum_tax, sum(in_sum) as in_sum, sum(totle_cost_tax)-sum(totle_cost) as goods_tax from v_finance_carr_purc_deta";
	String strWhere = RequestUtil.getStringParameter(request ,"where_sql");
	String strValue = RequestUtil.getStringParameter(request ,"where_value");
	String strType = RequestUtil.getStringParameter(request ,"where_type");

	if  (!strWhere.equals("")) {
		strWhere += " group by v_finance_carr_purc_deta.provider_id,v_finance_carr_purc_deta.cost_unit_id,v_finance_carr_purc_deta.provider_code,v_finance_carr_purc_deta.provider_name,v_finance_carr_purc_deta.material_diff )";
	} else {
		strWhere = " group by v_finance_carr_purc_deta.provider_id,v_finance_carr_purc_deta.cost_unit_id,v_finance_carr_purc_deta.provider_code,v_finance_carr_purc_deta.provider_name,v_finance_carr_purc_deta.material_diff )";
	}

	BaseDAO dao = BaseDAO.getInstance();

	Map mpCnt = null;

	System.out.println("--------v_finance_carr_purc_deta SUM-IN SQL ====="+strSQL+strWhere);


	if (strWhere.indexOf("notDoQuery") == -1) {
		if (!strValue.equals("")) {
			mpCnt = dao.queryOneRow(strSQL + " where " + strWhere ,new String[]{strValue ,strType});
		} else {
			mpCnt = dao.queryOneRow(strSQL + " where " + strWhere);
		}
	} else {
		mpCnt = FactoryUtil.newMap();
	}

	//System.out.println("mpCnt = " + mpCnt);
	/*(account_flag = ? and (cost_unit_id = ?))
	[EAM2004] 2004-10-11 23:18:21 strCntSQL = select sum(cntnum) as pagenum from (select in_material.provider_code ,in_material.provider_name ,in_material.cost_unit_id ,count(provider_id) as cntnum ,in_material.provider_id  from in_material WHERE  (cost_unit_id = ?) and ((in_material.auditing ='1' and in_material.is_audited='2' and (is_record='0' or is_record is null)))  group by in_material.provider_id,in_material.cost_unit_id,in_material.provider_code,in_material.provider_name order by provider_id)
	*/
%>

<SCRIPT LANGUAGE="JavaScript">
<!--
/**
*	
*/
function f_gridDblFunc(keyID) {
	var curKeyID = trselObj.f_get_select_key();
	curKeyID = curKeyID[0];
	if (curKeyID == "") return ;
	//alert("curKeyID = " + curKeyID);

	var carriage_mode = trselObj.f_getTDValueByKeyName(curKeyID ,"v_finance_carr_purc_deta.carriage_mode");
	var provider_id = trselObj.f_getTDValueByKeyName(curKeyID ,"v_finance_carr_purc_deta.provider_id");
	var cost_unit_id = trselObj.f_getTDValueByKeyName(curKeyID ,"v_finance_carr_purc_deta.cost_unit_id");
	var matdiff = trselObj.f_getTDValueByKeyName(curKeyID ,"v_finance_carr_purc_deta.material_diff");
//	var aflag = trselObj.f_getTDValueByKeyName(curKeyID ,"v_finance_carr_purc_deta.account_flag");
	//alert("aflag=" + aflag);
	//var trObj = trselObj.f_getSelectTR();
	//carriage_mode = trObj.cells(4).innerText;

//	var where_sql = " finance_carr_purc_deta.is_audited = '2' and finance_carr_purc_deta.provider_id = ? and finance_carr_purc_deta.carriage_mode = ? and finance_carr_purc_deta.cost_unit_id = ? and finance_carr_purc_deta.material_diff = ? ";
	var where_sql = " provider_id = ? and carriage_mode = ? and cost_unit_id = ? and material_diff = ? ";
	var where_type = "string;string;string;string";
	var where_value = provider_id + ";" + carriage_mode+";"+cost_unit_id + ";" + matdiff;
	//alert("curKeyID"+curKeyID);
	//alert("carriage_mode"+carriage_mode);

	var yearMonth = document.all("extend_param").value;
	if (yearMonth != "") {
		var whereMonth = yearMonth.split("-")[1];
		//alert(whereMonth);
		var whereYear = yearMonth.split("-")[0];
		if (whereMonth != "") {
			var aflag = whereYear + "-" + whereMonth;
			where_sql += " and finance_carr_purc_deta.account_flag = '"+ aflag +"'";
		}
	}

//	if (aflag != "") {
//		where_sql += " and account_flag = '"+ aflag +"'";
//	} else {
//		where_sql += " and account_flag is null";
//	}

	var contextPath = "<%=CONTEXTPATH%>";
	var funcid = "GROUPAUDIT_CARR_PURC";

	//var href = contextPath + g_layoutForm + funcid + "&pageType=subgrid";
	var href = contextPath + "/wzmis/finance/frame_groupaudit_purc_carr.jsp?funcid=" + funcid + "&pageType=grid";
	href += "&where_sql=" + where_sql;
	href += "&where_value=" + where_value;
	href += "&where_type=" + where_type;
//	href += "&orgwhere_sql=" + where_sql;
//	href += "&orgwhere_type=" + where_type;
//	href += "&orgwhere_value=" + where_value;
	//alert("where_sql = " + where_sql);
	//alert("where_value = " + where_value);

	parent.frames("frmformdata").document.location = href
}

function f_setGrid() {
	f_setAccountFlag();
}

function f_setAccountFlag() {
	//alert(document.all("extend_param").value);
	var yearObj = document.all("txtYear");
	var yearMonth = document.all("extend_param").value;

	var curdate = new Date();
	//alert(curdate.getYear());
	yearObj.value = curdate.getYear();

	var monthObj = document.all("selMonth");
	//alert(curdate.getMonth());
	var curMonth = curdate.getMonth() + 1;
	if (curMonth.length == 1) curMonth = "0" + curMonth;
	monthObj.value = "";//curMonth;

	if (yearMonth != "") {
		yearObj.value = yearMonth.split("-")[0];
		monthObj.value = yearMonth.split("-")[1];
	}
}

function f_month_where() {
	//alert(document.all("where_sql").value + "||" + document.all("orgwhere_sql").value);
	//var where_sql = "to_char(in_material.in_date ,'yyyy-mm') = ?";
	 var where_sql = "account_flag = ?";

	var orgwhere_sql = document.all("orgwhere_sql").value;
	var orgwhere_value = document.all("orgwhere_value").value;
	var orgwhere_type = document.all("orgwhere_type").value;

	if (orgwhere_sql == "") {
		orgwhere_sql = document.all("where_sql").value;
		orgwhere_value = document.all("where_value").value;
		orgwhere_type = document.all("where_type").value;

		document.all("orgwhere_sql").value = orgwhere_sql;
		document.all("orgwhere_value").value = orgwhere_value;
		document.all("orgwhere_type").value = orgwhere_type;
	}

	var yearObj = document.all("txtYear");
	var monthObj = document.all("selMonth");

	var where_value = yearObj.value + "-" + monthObj.value;
	document.all("extend_param").value = where_value;
	var where_type = "string";

	if (orgwhere_sql != "") {
		where_sql = where_sql + " and (" + orgwhere_sql + ")";
		where_value += ";" + orgwhere_value;
		where_type += ";" + orgwhere_type;
	}

	if (monthObj.value == "") {
		where_sql = orgwhere_sql;
		where_value = orgwhere_value;
		where_type = orgwhere_type;
	}

	document.all("where_sql").value = where_sql;
	document.all("where_value").value = where_value;
	document.all("where_type").value = where_type;

	var contextPath = "<%=CONTEXTPATH%>";
	var funcid = "GROUPAUDIT_CARR_PURC";

	var href = contextPath + g_layoutForm + funcid + "&pageType=subgrid";
	href += "&where_sql=" + "notDoQuery";
	href += "&where_value=" + "";
	href += "&where_type=" + "";
	href += "&orgwhere_sql=" + "notDoQuery";
	href += "&orgwhere_type=" + "";
	href += "&orgwhere_value=" + "";
	//alert("href = " + href);

	parent.frames("frmformdata").document.location = href



	f_reloadPage();
}
//-->
</SCRIPT>
<table class="gridTbl">
<tr class="gridData">
	<td class="gridData" >
	<eam:message key="account.flag" />: 
	<input type="text" name="txtYear" id="txtYear" size="5" value=""> -
	<select name="selMonth" id="selMonth" onchange="f_month_where(this);">
	<option value=""></option>
	<option value="01">01</option>
	<option value="02">02</option>
	<option value="03">03</option>
	<option value="04">04</option>
	<option value="05">05</option>
	<option value="06">06</option>
	<option value="07">07</option>
	<option value="08">08</option>
	<option value="09">09</option>
	<option value="10">10</option>
	<option value="11">11</option>
	<option value="12">12</option>
	</select>
	<eam:message key="column.GROUPAUDIT_PROVIDER_V.view_groupaudit_provider.num" />:<%=(String)mpCnt.get("pagenum")%>
	<eam:message key="column.GROUPAUDIT_PROVIDER_V.view_groupaudit_provider.goods_sum" />:<%=(String)mpCnt.get("goods_sum")%>
	<eam:message key="column.GROUPAUDIT_PROVIDER_V.view_groupaudit_provider.goods_sum_tax" />:<%=(String)mpCnt.get("goods_sum_tax")%>
	<eam:message key="column.GROUPAUDIT_PROVIDER_V.view_groupaudit_provider.goods_tax" />:<%=(String)mpCnt.get("goods_tax")%>
	<eam:message key="column.GROUPAUDIT_PROVIDER_V.view_groupaudit_provider.in_sum" />:<%=(String)mpCnt.get("in_sum")%>
	</td>
</tr>
</table>